<?php
/**
 * Http响应封装
 *
 * @version $Id: IHttpResponse.php 312 2008-04-23 16:29:38Z iwind $
 * @package util.net
 * @since 0.0.3
 */
class IHttpResponse extends IObject {
	private $fullText;
	private $headers = array();
	private $status;
	private $httpVersion;
	private $body;
	
	/**
	 * 构造器
	 *
	 * @param string $responseString 响应字符串
	 * @since 0.0.3
	 */
	public function __construct($responseString) {
		$this->fullText = $responseString;
		list($header, $body) = preg_split("/(\r?\n){2}/s", $responseString, 2);
		$this->body = $body;
		$lines = preg_split("/\r?\n/s", $header);
		foreach ($lines as $index=>$line) {
			$match = array();
			if (preg_match("/^HTTP\\/(1\\.\\d) (\\d+) /", $line, $match)) {
				$this->httpVersion = $match[1];
				$this->status = $match[2];
			}
			if (strstr($line, ":")) {
				list($headerName, $headerValue) = explode(":", $line, 2);
				$this->headers[strtolower($headerName)] = $headerValue;
			}
		}
	}
	
	/**
	 * 取得响应的全文
	 *
	 * 包含报头
	 * 
	 * @return string
	 * @since 0.2
	 */
	public function getFullText() {
		return $this->fullText;
	}
	
	/**
	 * 取得Header信息
	 *
	 * @return array
	 * @since 0.0.3
	 */
	public function getHeaders() {
		return $this->headers;
	}
	
	/**
	 * 取得单个Header信息
	 *
	 * @param string $name 名称
	 * @return string
	 * @since 0.0.3
	 */
	public function getHeader($name) {
		if (isset($this->headers[$name])) {
			return $this->headers[$name];
		}
		return null;
	}
	
	/**
	 * 取得状态码
	 * 
	 * 200, 301 ...
	 *
	 * @return integer
	 * @since 0.0.3
	 */
	public function getStatus() {
		return $this->status;
	}
	
	/**
	 * 取得主体
	 *
	 * @return string
	 * @since 0.0.3
	 */
	public function getBody() {
		return $this->body;
	}
	
	/**
	 * 取得Http版本
	 *
	 * @return float
	 * @since 0.0.3
	 */
	public function getHttpVersion() {
		return $this->httpVersion;
	}
}

?>